﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Gencode.Common.Generics;

namespace Gencode.Gengui.Wizzard
{
	public partial class WizzardDialog : GUForm, IWizardControler
    {
        Dictionary<string, WizzardControl> controls;
		WizzardControl currentControl;
		TypeContainer dataObjects;
		bool setButtons = true;

        public WizzardDialog()
        {
            InitializeComponent();
			controls = new Dictionary<string, WizzardControl>();
			dataObjects = new TypeContainer();

            SetButtons();
			//RepositionButtons();
        }

		/// <summary>
		/// Sets the buttons-enabled value to the given value
		/// </summary>
		public bool ButtonsEnabled
		{
			set
			{
				if (value == false)
				{
					btnCancle.Enabled = EnableCanleButton;
					btnFinish.Enabled = value;
					btnNext.Enabled = value;
					btnPrevious.Enabled = value;

					setButtons = false;
				}
				else
				{
					setButtons = true;
					btnCancle.Enabled = EnableCanleButton;
					SetButtons();
				}
			}
		}

		/// <summary>
		/// Container for Data
		/// </summary>
		public TypeContainer DataObjects
		{
			get
			{
				return dataObjects;
			}
		}

        private void SetButtons()
        {
			if (setButtons)
			{
				if (currentControl != null && controls.Count > 0)
				{
					if (currentControl.PreviousControl == string.Empty)
					{
						btnPrevious.Enabled = false;
					}
					else
					{
						btnPrevious.Enabled = true;
					}

					if (currentControl.NextControl == string.Empty)
					{
						btnNext.Enabled = false;
						btnFinish.Enabled = true;
						btnCancle.Enabled = false;
					}
					else
					{
						btnNext.Enabled = true;
						btnFinish.Enabled = false;
						btnCancle.Enabled = EnableCanleButton;
					}
				}
				else
				{
					btnFinish.Enabled = false;
					btnNext.Enabled = false;
					btnPrevious.Enabled = false;
				}
			}
        }

		//protected void RepositionButtons()
		//{
		//    if ((this.Width - btnCancle.Width - btnFinish.Width - btnNext.Width - btnPrevious.Width - 50) > 0)
		//    {
		//        int X = (this.Width - btnCancle.Width) - 20;
		//        btnCancle.Location = new Point(X, btnCancle.Location.Y);
		//        X -= btnFinish.Width + 5;
		//        btnFinish.Location = new Point(X, btnFinish.Location.Y);
		//        X -= btnNext.Width + 20;
		//        btnNext.Location = new Point(X, btnNext.Location.Y);
		//        X -= btnPrevious.Width;
		//        btnPrevious.Location = new Point(X, btnPrevious.Location.Y);

		//    }
		//    else
		//    {
		//        int Y = 10;
		//        btnPrevious.Location = new Point(btnPrevious.Location.Y, Y);
		//        btnNext.Location = new Point(btnNext.Location.Y, Y + btnPrevious.Width);
		//        btnFinish.Location = new Point(btnFinish.Location.Y, Y + btnPrevious.Width + btnNext.Width + 20);
		//        btnCancle.Location = new Point(btnCancle.Location.Y, Y + btnPrevious.Width + btnNext.Width + 30 + btnFinish.Width);
		//    }
		//}

		/// <summary>
		/// Adds a Control to the dialog
		/// </summary>
		/// <param name="key">keyname of the dialog</param>
		/// <param name="control">control to add</param>
        public void AddControl(string key, WizzardControl control)
        {
			control.Dock = DockStyle.Fill;
			controls.Add(key, control);

			if (controls.Count == 1)
			{
				currentControl = control;
				ControlPanel.Panel1.Controls.Add(currentControl);
			}
			SetButtons();
        }

		/// <summary>
		/// Initializes the control that is referenced witu curentControl
		/// </summary>
		protected virtual void InitializeCurentControl()
		{
			currentControl.InitializeWizzardControl();
			
			SetButtons();
		}

		/// <summary>
		/// Moves to next view. Interface extension that allows views to navigate. Only works if button is enabled
		/// </summary>
		public virtual void MoveNext()
		{
			if (btnNext.Enabled)
				btnNext_Click(null, null);
		}

		/// <summary>
		/// Moves to previous view. Interface extension that allows views to navigate. Only works if button is enabled
		/// </summary>
		public virtual void MovePrevious()
		{
			if (btnPrevious.Enabled)
				btnPrevious_Click(null, null);
		}

		/// <summary>
		/// Gets executed when finish is clicked. has to be overriden
		/// </summary>
		public virtual void Finish()
		{
		}

        private void btnFinish_Click(object sender, EventArgs e)
        {
			if (currentControl.Finish())
				Finish();
        }

        private void btnNext_Click(object sender, EventArgs e)
        {
			if (currentControl.MoveNext())
			{
				ControlPanel.Panel1.Controls.Remove(currentControl);

				if (currentControl.NextControl != string.Empty)
					currentControl = controls[currentControl.NextControl];

				currentControl.InitializeWizzardControl();
				ControlPanel.Panel1.Controls.Add(currentControl);

				SetButtons();
			}
        }

        private void btnPrevious_Click(object sender, EventArgs e)
        {
			if (currentControl.MovePrevious())
			{
				ControlPanel.Panel1.Controls.Remove(currentControl);

				if (currentControl.PreviousControl != string.Empty)
					currentControl = controls[currentControl.PreviousControl];

				ControlPanel.Panel1.Controls.Add(currentControl);

				SetButtons();
			}
        }

        private void btnCancle_Click(object sender, EventArgs e)
        {
            this.Close();
        }		

		bool _enableCanleButton = false;
		public bool EnableCanleButton
		{
			get
			{
				return _enableCanleButton;
			}
			set
			{
				_enableCanleButton = value;
				btnCancle.Enabled = value;
			}
		}
	}
}
